#!/bin/bash
#
# Quick 5-minute sanity check without database verification
# Focus on client-side metrics consistency
#

set -e

echo "=============================================="
echo "  Quick Sanity Check (No DB verification)"
echo "=============================================="
echo ""

# Configuration
TOTAL_RECORDS=10000
BATCH_SIZE=500
CONCURRENCY=2

echo "[1/4] Configuration"
echo "  Records: $TOTAL_RECORDS"
echo "  Batch: $BATCH_SIZE"
echo "  Threads: $CONCURRENCY"
echo ""

# Build
echo "[2/4] Building..."
cd /home/xiaodouzi/workspace/422a67685e23f903e048434351289787/build
make bench_write -j4 > /dev/null 2>&1
echo "  ✓ Build complete"
echo ""

# Start server
echo "[3/4] Starting server..."
killall -9 ingestd 2>/dev/null || true
sleep 1
cd /home/xiaodouzi/workspace/422a67685e23f903e048434351289787
./build/server/ingestd/ingestd > /tmp/server.log 2>&1 &
SERVER_PID=$!
sleep 2

if ! kill -0 $SERVER_PID 2>/dev/null; then
    echo "  ✗ Server failed to start"
    exit 1
fi
echo "  ✓ Server running (PID: $SERVER_PID)"
echo ""

# Test UDS
echo "[4a] Testing UDS (Unix Domain Socket)..."
INGEST_ENDPOINT=local ./build/bench/cpp/bench_write \
    $TOTAL_RECORDS $BATCH_SIZE $CONCURRENCY 2>&1 | tee /tmp/uds.log
echo ""

# Extract UDS metrics
UDS_QPS=$(grep "QPS:" /tmp/uds.log | awk '{print $2}')
UDS_SUCCESS=$(grep "Success:" /tmp/uds.log | awk '{print $2}')
UDS_DUR=$(grep "Duration:" /tmp/uds.log | awk '{print $2}')

sleep 2

# Test TCP
echo "[4b] Testing TCP (Loopback)..."
INGEST_ENDPOINT=127.0.0.1:9090 ./build/bench/cpp/bench_write \
    $TOTAL_RECORDS $BATCH_SIZE $CONCURRENCY 2>&1 | tee /tmp/tcp.log
echo ""

# Extract TCP metrics
TCP_QPS=$(grep "QPS:" /tmp/tcp.log | awk '{print $2}')
TCP_SUCCESS=$(grep "Success:" /tmp/tcp.log | awk '{print $2}')
TCP_DUR=$(grep "Duration:" /tmp/tcp.log | awk '{print $2}')

# Stop server
kill $SERVER_PID 2>/dev/null || true
sleep 1

# Analysis
echo "=============================================="
echo "  Analysis"
echo "=============================================="
echo ""
echo "UDS Results:"
echo "  QPS:      $UDS_QPS"
echo "  Success:  $UDS_SUCCESS points"
echo "  Duration: ${UDS_DUR}s"
echo ""
echo "TCP Results:"
echo "  QPS:      $TCP_QPS"
echo "  Success:  $TCP_SUCCESS points"
echo "  Duration: ${TCP_DUR}s"
echo ""

# Validate point counts
if [ "$UDS_SUCCESS" != "$TOTAL_RECORDS" ]; then
    echo "⚠️  UDS: Expected $TOTAL_RECORDS but got $UDS_SUCCESS"
fi

if [ "$TCP_SUCCESS" != "$TOTAL_RECORDS" ]; then
    echo "⚠️  TCP: Expected $TOTAL_RECORDS but got $TCP_SUCCESS"
fi

# Calculate ratio
if [ -n "$TCP_QPS" ] && [ -n "$UDS_QPS" ] && [ "$TCP_QPS" != "0" ]; then
    RATIO=$(echo "scale=2; $UDS_QPS / $TCP_QPS" | bc)
    echo "UDS/TCP Ratio: ${RATIO}x"
    echo ""

    # Check ratio reasonableness
    IS_TOO_HIGH=$(echo "$RATIO > 10" | bc -l)
    IS_TOO_LOW=$(echo "$RATIO < 1.1" | bc -l)

    if [ "$IS_TOO_HIGH" = "1" ]; then
        echo "❌ FAIL: Ratio ${RATIO}x is suspiciously high!"
        echo "   Normal UDS advantage is 1.2x-3x"
        echo "   Possible issues:"
        echo "     - Different code paths"
        echo "     - Counting batches instead of points"
        echo "     - Configuration mismatch"
    elif [ "$IS_TOO_LOW" = "1" ]; then
        echo "⚠️  WARNING: Ratio ${RATIO}x is too small"
        echo "   UDS should be faster than TCP"
    else
        echo "✅ PASS: Ratio ${RATIO}x is reasonable"
    fi
else
    echo "Unable to calculate ratio"
fi

echo ""
echo "Server log (last 10 lines):"
tail -10 /tmp/server.log | grep -E "(MONITOR|WriteStream|points/sec)" || echo "No monitoring output"
echo ""
echo "=============================================="
